<!--
 * @Description:业务系统
-->
<template>
  <div class="registered-container statisticalQuery appMainWarp">
    <el-form :inline="true" :model="searchKey" class="demo-form-inline">

      <el-form-item label="操作时间:">
        <el-date-picker
          v-model="searchKey.time"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
        />
      </el-form-item>

      <el-form-item label="患者id:">
        <el-input v-model="searchKey.pat_id" placeholder="请输入患者id" @change="suborder_no" />
      </el-form-item>

      <el-form-item label="患者姓名:">
        <el-input v-model="searchKey.pat_name" placeholder="请输入患者姓名" @change="suborder_no" />
      </el-form-item>

      <el-form-item label="交易单号:">
        <el-input v-model="searchKey.order_no" placeholder="请输入交易单号" @change="suborder_no" />
      </el-form-item>

      <el-form-item label="业务类型:">
        <el-cascader
          v-model="searchKey.bill_type"
          :options="searchKey.BillType"
          :props="props"
          change-on-select
          clearable
        />
      </el-form-item>

      <el-form-item label="支付方式:">
        <el-select v-model="searchKey.pay_style" placeholder="请选择">
          <el-option
            v-for="item in searchKey.PayStyle"
            :key="item.no"
            :label="item.name"
            :value="item.no"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="终端编码:">
        <el-select v-model="searchKey.zzj_id" placeholder="请选择">
          <el-option
            v-for="item in searchKey.zzj_idList"
            :key="item.index"
            :label="item.zzj_id"
            :value="item.zzj_id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="onLoad">查询</el-button>
      </el-form-item>
    </el-form>

    <template>
      <el-table
        ref="table"
        v-el-height-adaptive-table="{bottomOffset: 90}"
        max-height="500px"
        center
        class="table"
        :data="searchDate"
        stripe
        border
      >
        <el-table-column label="编号" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">
              {{ scope.row.id||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="交易状态" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.RespInfo||'-' }}</div></template>
        </el-table-column>
        <el-table-column label="银行卡号" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.idCard ||'-' }}</div></template>
        </el-table-column>
        <el-table-column label="交易金额" align="center">
          <template slot-scope="scope"> <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.cash||'-' }}</div></template>
        </el-table-column>
        <el-table-column label="自助机订单号" align="center">
          <template slot-scope="scope"> <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.out_trade_no||'-' }}</div></template>
        </el-table-column>
        <el-table-column label="银联流水号" align="center">
          <template slot-scope="scope"> <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.RespTrace||'-' }}</div></template>
        </el-table-column>
        <el-table-column label="交易日期" align="center">
          <template slot-scope="scope"> <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.TransDate||'-' }}</div></template>
        </el-table-column>
        <el-table-column label="交易时间" align="center">
          <template slot-scope="scope"> <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.TransTime||'-' }}</div></template>
        </el-table-column>
        <el-table-column label="系统参考号" align="center">
          <template slot-scope="scope"> <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.Ref||'-' }}</div></template>
        </el-table-column>
        <el-table-column label="银联订单号" align="center">
          <template slot-scope="scope"> <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.trade_no||'-' }}</div></template>
        </el-table-column>
        <el-table-column label="患者id" align="center">
          <template slot-scope="scope"> <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.pat_id||'-' }}</div></template>
        </el-table-column>
        <el-table-column label="患者姓名" align="center">
          <template slot-scope="scope"> <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.pat_name||'-' }}</div></template>
        </el-table-column>

        <el-table-column label="支付方式" align="center">
          <template slot-scope="scope"> <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.pay_type||'-' }}</div></template>
        </el-table-column>
        <el-table-column label="交易类型" align="center">
          <template slot-scope="scope"> <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.business_type||'-' }}</div></template>
        </el-table-column>

        <el-table-column label="机器编号" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">
              {{ scope.row.zzj_id||'-' }}
            </div>
          </template>
        </el-table-column>

        <el-table-column label="系统日期" align="center">
          <template slot-scope="scope"><div :class="[scope.row.RespCode !== '00' ? 'abnormal':'normal']">{{ scope.row.op_date||'-' }}</div></template>
        </el-table-column>

      </el-table>
    </template>
    <pagination
      :total="page.totalRow"
      :page.sync="page.currentPage"
      :limit.sync="page.pageSize"
      @pagination="onSearch"
    />

  </div>
</template>

<script>
import elHeightAdaptiveTable from '@/directive/el-table'
import { getConfigType } from '@/api/transactRecords/index'

import Pagination from '@/components/Pagination' // 分页

export default {
  name: 'StatisticalQuery',
  components: {
    Pagination
  },
  directives: { elHeightAdaptiveTable },
  data() {
    return {
      quaflat: 0, // 按钮默认
      date: 1, // 按天月年
      searchKey: {
        time: '', // 时间
        pat_id: '', // 患者id
        pat_name: '', // 患者姓名
        order_no: '', // 订单号
        trans_type: '', // 交易类型
        compensationWayList: [
          { id: '', name: '全部' },
          { id: 1, name: '正交易' },
          { id: 2, name: '负交易' }
        ],
        pay_style: '', // 支付方式
        PayStyle: [],
        zzj_id: '', // 总端编码
        zzj_idList: [],
        bill_type: [], // 业务类型
        BillType: []
      },
      RespCode: '',

      props: {
        label: 'name',
        value: 'no',
        children: 'data'
      },
      start_time: '',
      end_time: '',

      searchDate: [],

      page: {
        tableLoading: false,
        currentPage: 1, // 当前页
        pageSize: 20, // 默认条数
        totalRow: 0, // 总条数
        totalPage: 0 // 总页数
      },
      pickerOptions: { // 日期不能点击
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      system: {}
    }
  },

  created() {
  },
  mounted() {
    this.getConfig()
    this.onLoad()
  },
  methods: {
    // 系统配置搜索
    getConfig(type, val) {
      var that = this
      var params = {}
      getConfigType(params).then(res => {
        // 院区
        var defaultValue = [{ id: '', no: '', name: '全部' }]
        var temp = res.data.Hospital
        var hospitalData = [...defaultValue, ...temp]
        that.searchKey.Hospital = hospitalData
        // 支付方式
        var tempPay = res.data.PayStyle
        var PayStyle = [...defaultValue, ...tempPay]
        that.searchKey.PayStyle = PayStyle
        that.searchKey.System = res.data.System
        that.searchKey.BillType = that.getTreeData(res.data.BillType)
      })
        .catch(() => {
        })
    },
    // 递归判断列表，把最后的children的内容不显示
    getTreeData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].data.length < 1) {
          // children若为空数组，则将children设为undefined
          data[i].data = undefined
        } else {
          // children若不为空数组，则继续 递归调用 本方法
          this.getTreeData(data[i].data)
        }
      }
      return data
    },
    // 查询列表
    onLoad() {
      this.page.currentPage = 1
      this.onSearch()
    },
    onSearch() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.start_time = that.searchKey.time[0]
        params.end_time = that.searchKey.time[1]
      } else {
        params.start_time = ''
        params.end_time = ''
      }
      params.pat_id = that.searchKey.pat_id
      params.pat_name = that.searchKey.pat_name
      params.order_no = that.searchKey.order_no
      params.bill_type =
        that.searchKey.bill_type.length >= 1 ? that.searchKey.bill_type[1] : ''
      params.zzj_id = that.searchKey.zzj_id
      params.pay_style = that.searchKey.pay_style
      that.page.tableLoading = true
      selfList(params)
        .then(res => {
          that.page.tableLoading = false
          that.searchDate = res.data.result
          that.system = res.data.system_no
          var defaultValue = [{ zzj_id: '全部' }]
          var temp = res.data.zzj_id
          var zzj_id = [...defaultValue, ...temp]
          that.searchKey.zzj_idList = zzj_id
          that.RespCode = res.data.RespCode
          that.page.totalPage = res.data.pageSum
          that.page.totalRow = parseInt(res.data.count)
        })
        .catch(() => {
          that.page.tableLoading = false
        })
    },

    // 验证订单号
    suborder_no() {
      const id = this.searchKey.order_no
      const reg = /^[0-9a-zA-Z]+$/
      if (!reg.test(id)) {
        this.$message({
          type: 'warning',
          message: '请输入正确的订单号'
        })
        this.searchKey.order_no = ''
        return false
      }
    }
    // getlisttable(flat) {
    //   this.quaflat = flat
    //   if (this.quaflat === 0) {
    //     this.date = 1
    //     this.getDate()
    //     this.onLoad()
    //   } else if (this.quaflat === 1) {
    //     this.date = 2
    //     this.getWeek()
    //     this.onLoad()
    //   } else {
    //     this.date = 3
    //     this.getMonth()
    //     this.onLoad()
    //   }
    // },
    // // 获取昨日
    // getDate() {
    //   var dateTime = []
    //   var today = new Date()
    //   var yesterday = new Date(today.setTime(today.getTime() - 3600 * 1000 * 24 * 1))
    //   var y = yesterday.getFullYear()
    //   var m = yesterday.getMonth() + 1
    //   var d = yesterday.getDate()
    //   // var start_times = y+'-'+m+'-'+d+' 00:00:00';//开始
    //   // var end_time = y+'-'+m+'-'+d+' 23:59:59';//结束
    //   this.start_time = y + '-' + m + '-' + d// 开始
    //   this.end_time = y + '-' + m + '-' + d// 结束
    //   this.searchKey.time = [this.start_time, this.end_time]
    // },
    // // 获取最近七天
    // getWeek() {
    //   var today = new Date()
    //   var yesterday = new Date(today.setTime(today.getTime() - 3600 * 1000 * 24 * 7))
    //   var y = yesterday.getFullYear()
    //   var m = yesterday.getMonth() + 1
    //   var d = yesterday.getDate()
    //   var year = new Date().getFullYear()
    //   var month = new Date().getMonth() + 1
    //   var day = new Date().getDate()
    //   // var start_times = y+'-'+m+'-'+d+' 00:00:00';//开始
    //   // var end_time = y+'-'+m+'-'+d+' 23:59:59';//结束
    //   this.start_time = y + '-' + m + '-' + d// 开始
    //   this.end_time = year + '-' + month + '-' + day// 结束
    //   this.searchKey.time = [this.start_time, this.end_time]
    // },
    // // 获取月
    // getMonth() {
    //   var today = new Date()
    //   var yesterday = new Date(today.setTime(today.getTime() - 3600 * 1000 * 24 * 30))
    //   var y = yesterday.getFullYear()
    //   var m = yesterday.getMonth() + 1
    //   var d = yesterday.getDate()
    //   var year = new Date().getFullYear()
    //   var month = new Date().getMonth() + 1
    //   var day = new Date().getDate()
    //   // var start_times = y+'-'+m+'-'+d+' 00:00:00';//开始
    //   // var end_time = y+'-'+m+'-'+d+' 23:59:59';//结束
    //   this.start_time = y + '-' + m + '-' + d// 开始
    //   this.end_time = year + '-' + month + '-' + day// 结束
    //   this.searchKey.time = [this.start_time, this.end_time]
    // }
  }
}
</script>

<style lang="scss" scoped>
.statisticalQuery {
  // position: relative;
  // width: 100%;
  .demo-form-inline {
    .el-button--primary {
      background-color: #579ef8 !important;
      color: #fff !important;
    }
   .el-button {
      background-color: #e8f4ff;
      color: #579ef8;
      // border: 1px solid rgb(77, 77, 77);
    }
  }
  .abnormal{
    color: red;
    // text-decoration: underline;
  }
  .normal{
    color:rgb(113, 118, 133);
    cursor: pointer;
  }

}
</style>

